package com.taotao.wechat.business;

import com.rop.client.CompositeResponse;
import com.taotao.admin.MyRopClient;
import com.taotao.rop.database.Constant;
import com.taotao.rop.database.domain.WechatRequest;
import com.taotao.rop.database.domain.WechatUser;
import com.taotao.rop.response.BaseResponse;
import com.taotao.wechat.message.MessageEntry;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Request {
    public static void insertRequest(String appKey, String openId, Map<String, String> requestMap, String respMessage){
        WechatUser user = new WechatUser();
        user.setAppId(appKey);
        user.setOpenId(openId);
        user.setStatus(Constant.STATUS_ENABLE);
        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient("").buildClientRequest()
                .post(user, BaseResponse.class, "wechatUser.listAll", "1.0");
        if (response.isSuccessful()) {
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()) {
                List<LinkedHashMap<String, Object>> respData = (List<LinkedHashMap<String, Object>>) resp.getData();
                Long userId = Constant.OLD_USER_ID;
                if (respData != null && respData.size() > 0) {
                    LinkedHashMap<String, Object> theUser = respData.get(0);
                    userId = Long.parseLong(theUser.get("id").toString());
                }
                //以上获取发送用户的id，为了下面储存用户请求信息
                WechatRequest request = new WechatRequest();
                request.setUserId(userId);
                request.setMediaId(openId); //使用mediaId字段存储openId
                request.setMsgId(appKey); //使用msgid字段存储appId
                request.setCreateTime(new Date());
                request.setMsgType(requestMap.get(MessageEntry.MsgType));
                request.setContent(requestMap.get(MessageEntry.Content));
                String voice = requestMap.get(MessageEntry.RECOGNITION);
                if (StringUtils.isNotBlank(voice)) {
                    request.setContent(voice);
                }
                if (requestMap.get(MessageEntry.Location_X) != null) {
                    request.setLocationX(Double.parseDouble(requestMap.get(MessageEntry.Location_X)));
                }
                if (requestMap.get(MessageEntry.Location_Y) != null) {
                    request.setLocationY(Double.parseDouble(requestMap.get(MessageEntry.Location_Y)));
                }
                if (requestMap.get(MessageEntry.Scale) != null) {
                    request.setScale(Double.parseDouble(requestMap.get(MessageEntry.Scale)));
                }
                request.setLabel(requestMap.get(MessageEntry.Label));
                request.setEvent(requestMap.get(MessageEntry.Event));
                if (requestMap.get("EventKey") != null) {
                    request.setEventkey(requestMap.get("EventKey"));
                }
                request.setEventkey(requestMap.get("EventKey"));
                if (requestMap.get("key") != null) { //地理位置
                    request.setEventkey(requestMap.get("key"));
                }
                request.setRespmsg(respMessage);
                MyRopClient.getRopClient("").buildClientRequest()
                        .post(request, BaseResponse.class, "wechatRequest.insert", "1.0");
            }
        }
    }
}
